package cn.frontop.funswitch.controller;

import cn.frontop.funswitch.common.Result;
import cn.frontop.funswitch.entity.ApiPathEntity;
import cn.frontop.funswitch.entity.IndexConfigEntity;
import cn.frontop.funswitch.entity.TokenEntity;
import cn.frontop.funswitch.param.JavaScriptParams;
import cn.frontop.funswitch.param.TestSql;
import cn.frontop.funswitch.service.ApiPathService;
import cn.frontop.funswitch.service.DatasourceService;
import cn.frontop.funswitch.service.IndexConfigService;
import cn.frontop.funswitch.service.TokenService;
import cn.frontop.funswitch.util.CommonUtil;
import cn.frontop.funswitch.util.JavaScriptUtil;
import cn.frontop.funswitch.util.SqlUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * @author Yun
 */
@RestController
@RequestMapping("/test")
@Api(tags = "测试接口")
public class TestController {
    @Autowired
    ApiPathService apiPathService;

    @Autowired
    TokenService tokenService;
    @Autowired
    IndexConfigService indexConfigService;
    @Autowired
    DatasourceService datasourceService;

    /**
     * 测试Token
     */
    @PostMapping("/token")
    @ApiOperation("测试Token 返回")
    public Result<Object> token(@RequestBody TokenEntity tokenEntity) {
        return Result.ok(tokenService.getTokenByEntity(tokenEntity));
    }

    /**
     * API接口测试
     */
    @PostMapping("/api")
    @ApiOperation("API接口测试")
    public Result<Object> api(HttpServletRequest request) {
        ApiPathEntity apiPathEntity = new ApiPathEntity();
        JSONObject httpJsonBody = CommonUtil.getHttpJsonBody(request);
        CommonUtil.isEmpty(httpJsonBody, "参数不合法!");
        BeanUtil.copyProperties(httpJsonBody, apiPathEntity);
        return Result.ok( apiPathService.apiTest(apiPathEntity, httpJsonBody));
    }

    /**
     * 指标测试
     */
    @PostMapping("/indexTest")
    @ApiOperation("指标测试")
    public Result<Object> indexTest(HttpServletRequest request) {
        JSONObject httpJsonBody = CommonUtil.getHttpJsonBody(request);
        IndexConfigEntity indexConfigEntity = new IndexConfigEntity();
        BeanUtil.copyProperties(httpJsonBody, indexConfigEntity);
        return Result.ok(indexConfigService.getIndexByEntity(indexConfigEntity, httpJsonBody));
    }

    /**
     * 测试 JS 脚本
     */
    @PostMapping("/testJS")
    @ApiOperation("测试JS")
    public Result<Object> delete(@RequestBody JavaScriptParams javaScriptParams) {
        return Result.ok(JavaScriptUtil.execution(javaScriptParams.getJs(), javaScriptParams.getSourceData(), javaScriptParams.getParams()));
    }

    /**
     * 测试响应速度
     */
    @PostMapping("/testApi")
    @ApiOperation("测试响应速度")
    public Result<Object> testApi(HttpServletRequest request) {
        return Result.ok(indexConfigService.testApi(CommonUtil.getHttpJsonBody(request)));
    }

    /**
     * 测试响应速度
     */
    @PostMapping("/testSql")
    @ApiOperation("测试SQL")
    public Result<Object> testSql(@RequestBody TestSql testSql) {
        return Result.ok(SqlUtil.execute(datasourceService.getById(testSql.getDataSourceId()), testSql.getSql()));
    }
}
